#!/usr/bin/perl -w

#=======================================================================
# $Id$
# Mixes the letters in words, except the first and last letter. Just an 
# experiment.
#
# Character set: UTF-8
# ©opyleft 2006– Øyvind A. Holm <sunny@sunbase.org>
# License: GNU General Public License, see end of file for legal stuff.
#=======================================================================

use strict;
use Getopt::Long;

$| = 1;

our $Debug = 0;

our %Opt = (
    'debug' => 0,
    'help' => 0,
    'version' => 0,
    'xml' => 0
);

our $progname = $0;
$progname =~ s#^.*/(.*?)$#$1#;

my $rcs_id = '$Id$';
my $id_date = $rcs_id;
$id_date =~ s/^.*?\d+ (\d\d\d\d-.*?\d\d:\d\d:\d\d\S+).*/$1/;

Getopt::Long::Configure("bundling");
GetOptions(
    "debug" => \$Opt{'debug'},
    "help|h" => \$Opt{'help'},
    "version" => \$Opt{'version'},
    "xml|x" => \$Opt{'xml'}
) || die("$progname: Option error. Use -h for help.\n");

$Opt{'debug'} && ($Debug = 1);
$Opt{'help'} && usage(0);
$Opt{'version'} && print_version();

while (<>) {
    s/(\W?\w)(\w{2,})(\w\W?)/join("", $1, mix_word($2), $3)/ge;
    print;
}

sub mix_word {
    my $Word = shift;
    D("mix_word(\"$Word\")");
    my @Array = split(//, $Word);
    D("Array = (\"" . join("\", \"", @Array) . "\")");
    mix_array(\@Array);
    return(join("", @Array));
}

sub mix_array {
	# {{{
	my $array = shift;
    D("mix_array(\"$array\")");
	my $i;
	for ($i = @$array; --$i; ) {
		my $j = int rand ($i+1);
		next if $i == $j;
		@$array[$i,$j] = @$array[$j,$i];
	}
	# }}}
} # mix_array()

sub print_version {
    # Print program version {{{
    xml_print("$rcs_id\n");
    exit(0);
    # }}}
} # print_version()

sub xml_print {
    # Print out some text, using DocBook if --xml is specified {{{
    my $Txt = shift;
    my ($xml_start, $xml_end) =
       (        "",       "");

    if ($Opt{'xml'}) {
        $xml_start = <<END;
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4//EN" "http://docbook.org/xml/4.3/docbookx.dtd">
<article>
  <screen>
END
        $xml_end = <<END;
</screen>
</article>
END
    }

    printf("%s%s%s",
        $xml_start,
        $Opt{'xml'} ? txt_to_xml($Txt) : "\n$Txt\n",
        $xml_end
    );
    # }}}
} # xml_print()

sub usage {
    # Send the help message to stdout {{{
    my $Retval = shift;

    xml_print(<<END);
$rcs_id

Mixes the letters in words, except the first and last letter. Just an 
experiment.

Usage: $progname [options] [file [files [...]]]

Options:

  -h, --help
    Show this help.
  --version
    Print version information.
  -x, --xml
    Create XML output.
  --debug
    Print debugging messages.
END
    exit($Retval);
    # }}}
} # usage()

sub D {
    # Print a debugging message {{{
    $Debug || return;
    my @call_info = caller;
    chomp(my $Txt = shift);
    my $File = $call_info[1];
    $File =~ s#\\#/#g;
    $File =~ s#^.*/(.*?)$#$1#;
    if ($Opt{'xml'}) {
        printf(STDERR "<debug> <pid>%s</pid> <file>%s</file> <line>%s</line> <msg>%s</msg> </debug>\n",
            txt_to_xml($$),
            txt_to_xml($File),
            txt_to_xml($call_info[2]),
            txt_to_xml($Txt)
        );
    } else {
        print(STDERR "$File:$call_info[2] $$ $Txt\n");
    }
    return("");
    # }}}
} # D()

sub txt_to_xml {
    # Return a XML-safe version of a string {{{
    my $Txt = shift;

    $Txt =~ s/&/&amp;/gs;
    $Txt =~ s/</&lt;/gs;
    $Txt =~ s/>/&gt;/gs;
    return($Txt);
    # }}}
} # txt_to_xml()

__END__

# Plain Old Documentation (POD) {{{

=pod

=head1 NAME

mixword

=head1 REVISION

$Id$

=head1 SYNOPSIS

mixword [options] [file [files [...]]]

=head1 DESCRIPTION

Mixes the letters in words, except the first and last letter. Just an 
experiment.

=head1 OPTIONS

=over 4

=item B<-h>, B<--help>

Print a brief help summary.

=item B<--version>

Print version information.

=item B<-x>, B<--xml>

Create XML output.

=item B<--debug>

Print debugging messages.

=back

=head1 BUGS



=head1 AUTHOR

Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.

=head1 COPYRIGHT

Copyleft © Øyvind A. Holm &lt;sunny@sunbase.org&gt;
This is free software; see the file F<COPYING> for legalese stuff.

=head1 LICENCE

This program is free software; you can redistribute it and/or modify it 
under the terms of the GNU General Public License as published by the 
Free Software Foundation; either version 2 of the License, or (at your 
option) any later version.

This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along 
with this program; if not, write to the Free Software Foundation, Inc., 
59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

=head1 SEE ALSO

=cut

# }}}

# vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :
# End of file $Id$
